package com.androidmvc.base.core;

import java.util.ArrayList;

public class UCore {
    private static final String LOG_TAG = "UCore";
    public static final int U_EXIT = 0; //应用退出

    private static UCore uCore = null;
    private DataCore dataCore;

    protected ArrayList<UIEventListener> eventListeners = new ArrayList<UIEventListener>(8);

    public synchronized static UCore getInstance() {
        if (uCore == null) {
            uCore = new UCore();
        }
        return uCore;
    }

    private UCore() {
        dataCore = DataCore.getInstance(this);
    }

    public void postCoreEvent(int event, Object data) {
        dataCore.postCoreEvent(event, data);
    }

    public void postCoreEvent(int event, Object data, boolean reset) {
        dataCore.postCoreEvent(event, data, reset);
    }

    public void postCoreEventAtFirst(int event, Object data) {
        dataCore.postCoreEventAtFirst(event, data);
    }

    public void addUIListener(UIEventListener listener) {
        if (!eventListeners.contains(listener)) {
            eventListeners.add(listener);
        }
    }

    public void removeUIListener(UIEventListener listener) {
        if (!eventListeners.isEmpty()) {
            eventListeners.remove(listener);
        }
    }

    public void clearUIListener() {
        eventListeners.clear();
    }

    public synchronized void notifyUIEvent(int event, Object data) {
        for (UIEventListener listener : eventListeners) {
            listener.onUIEvent(this, event, data);
        }
    }
}
